package lianxi;

import cn.tedu.yxc.JdbcUtils;
import com.sun.org.apache.xpath.internal.objects.XObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
public class CocoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("u");
        // 获取请求中键名为 p 的值
        String password = req.getParameter("p");
        // 获取请求中键名为 c 的值
        String code = req.getParameter("c");
        System.out.println(username);
        System.out.println(password);
        System.out.println(code);

        HttpSession session = req.getSession();
        Object c = session.getAttribute("C");
        boolean isTure = code !=null && code !=null && code.equals(c);
        if(!isTure){
            PrintWriter writer =resp.getWriter();
            writer.write("ccode is error");
            writer.flush();
            writer.close();
            return;
        }
        String sql = "SELECT `id` FROM `user` WHERE `username` = ? AND `password` = MD5(?) ";
        Long id = JdbcUtils.select(sql,Long.class, username, password);
        if(id !=null){
            //去首页http://localhost:8080/Yxc/index.jsp
            resp.sendRedirect("/Yxc/index.jsp");
        }else {
            resp.sendRedirect("/Yxc/login.jsp");
        }
//        String message = id !=null && id >0L? "登录成功！" : "登录失败！";
//        resp.setContentType("text/html;charset=utf-8");
//        PrintWriter writer = resp.getWriter();
//        writer.write(message);
//        writer.flush();
//        writer.close();
    }
}
